﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using DevExpress.XtraEditors;
using DataCentral.UsecaseControllers;
using DataCentral.BusinessDataObjects;
using System.Threading;
using CommonData;

namespace DataCentral
{
    public partial class LoginForm : CommonChildForm
    {
        private LoginUsecase _UCLogin;
        private UserBO userBO = null;
        public MainForm frmMain;
        public LoginForm(LoginUsecase _UCLogin)
        {
            InitializeComponent();
            this._UCLogin = _UCLogin;
        }

        #region My Functions and Methods

        

        private bool IsValidData()
        {
            bool isValid = true;
            errProvider.ClearErrors();

            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                CommonFunctions.ShowErrorProvider(errProvider,txtPassword, ConstantClass._Error_PassNotBlank);
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtUser.Text))
            {
                CommonFunctions.ShowErrorProvider(errProvider, txtUser, ConstantClass._Error_UserNotBlank);
                isValid = false;
            }
            if (!string.IsNullOrEmpty(txtPassword.Text) && !string.IsNullOrEmpty(txtUser.Text))
            {

                userBO = _UCLogin.GetUserBO(txtUser.Text.Trim(), CommonFunctions.EncodeTo64(txtPassword.Text));
                if (userBO == null)
                {
                    CommonFunctions.ShowErrorProvider(errProvider, txtUser, ConstantClass._Error_InvalidUser);
                    isValid = false;
                }
            }

            if (!isValid)
                MessageClass.ShowWarningMessage(ConstantClass._Error_InvalidData);
            return isValid;
        }

        private void WriteToAppConfig()
        {
            try
            {
                Properties.Settings.Default.APP_IsFirstUse = false;
                Properties.Settings.Default.RememberPassword= chkPassword.Checked;
                Properties.Settings.Default.RememberUserPass= chkUserPass.Checked;
                Properties.Settings.Default.UserLoginName = txtUser.Text;
                Properties.Settings.Default.UserLoginPassword = CommonFunctions.EncodeTo64(txtPassword.Text);
                Properties.Settings.Default.Save();                
            }
            catch
            {
            }
        }

        private void LoadConfigurationInfo()
        {            
            chkPassword.Checked = Properties.Settings.Default.RememberPassword;
            chkUserPass.Checked = Properties.Settings.Default.RememberUserPass;
            if (chkPassword.Checked)
                txtPassword.Text = CommonFunctions.DecodeFrom64(Properties.Settings.Default.UserLoginPassword);
            if (chkUserPass.Checked)
            {
                txtUser.Text = Properties.Settings.Default.UserLoginName;
                txtPassword.Text = CommonFunctions.DecodeFrom64(Properties.Settings.Default.UserLoginPassword);
            }
        }

        #endregion 

        private void LoginForm_Load(object sender, EventArgs e)
        {
            progressBar.Visible = false;
            LoadConfigurationInfo();
            txtUser.Focus();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (IsValidData())
            {
                this.Cursor = Cursors.WaitCursor;
                progressBar.Visible = true;
                progressBar.Refresh();
                WriteToAppConfig();
                _UCLogin.ShowMainForm(userBO);
                this.Close();
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void TextControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnLogin_Click(null, null);
        }

    }
}